<?php

/**
 * 前端授权用户model操作
 * @author  xiaobai
 * @date    2019-07-27
 */

namespace app\common\model;

use app\common\model\Common;

class User extends Common
{
    protected $pk                 = 'id';
    protected $autoWriteTimestamp = 'datetime'; // 自动写入时间，格式为Y-m-d H:i:s

    const SEX_M = 'M';
    const SEX_F = 'F';
    const SEX_O = 'O';

    /**
     * 保存授权用户信息
     * @param  string $provider  第三方平台参数
     * @param  string $openid    第三方平台用户openID
     * @param  array  $user_data 第三方授权用户信息
     * @return array
     */
    public function saveOauthUser($provider = '', $openid = '', $user_data = [])
    {
        if (!$provider || !$openid || !$user_data) return $this->result(1, '用户信息错误');
        if ($provider == 'weibo') {
            $nick_name  = $user_data['screen_name'];
            $avatar_url = $user_data['profile_image_url'];
            $avatar_hd  = $user_data['avatar_hd'];
            $sex        = $user_data['gender'] == 'n' ? self::SEX_O : strtoupper($user_data['gender']);
        } elseif ($provider == 'qq') {
            $nick_name  = $user_data['nickname'];
            $avatar_url = !empty($user_data['figureurl_qq_2']) ? $user_data['figureurl_qq_2'] : $user_data['figureurl_qq_1'];
            $avatar_hd  = $user_data['figureurl_qq_2'];
            $sex        = $user_data['gender'] == '女' ? self::SEX_F : self::SEX_M;
        }

        $data = [
            'provider'   => $provider,
            'openid'     => $openid,
            'nick_name'  => $nick_name,
            'sex'        => $sex,
            'avatar_url' => $avatar_url,
            'avatar_hd'  => $avatar_hd,
            'sync_time'  => date('Y-m-d H:i:s'),
        ];
        $res = $this->save($data);
        if ($res === false) {
            return $this->result(1, '保存用户信息失败');
        }

        $uid      = $this->id;
        $res_data = [
            'uid'        => $uid,
            'nick_name'  => $nick_name,
            'avatar_url' => $avatar_url,
        ];
        return $this->result(0, '', $res_data);
    }

    public function updateOauthUser($provider = '', $uid = 0, $user_data = [])
    {
        if (!$uid || !$user_data) return $this->result(1, '用户信息错误');
        if ($provider == 'weibo') {
            $nick_name  = $user_data['screen_name'];
            $avatar_url = $user_data['profile_image_url'];
            $avatar_hd  = $user_data['avatar_hd'];
            $sex        = $user_data['gender'] == 'n' ? self::SEX_O : strtoupper($user_data['gender']);
        } elseif ($provider == 'qq') {
            $nick_name  = $user_data['nickname'];
            $avatar_url = $user_data['figureurl_qq_1'];
            $avatar_hd  = $user_data['figureurl_qq_2'];
            $sex        = $user_data['gender'] == '女' ? self::SEX_F : self::SEX_M;
        }

        $where = ['id'=>$uid];
        $data  = [
            'nick_name'  => $nick_name,
            'sex'        => $sex,
            'avatar_url' => $avatar_url,
            'avatar_hd'  => $avatar_hd,
            'sync_time'  => date('Y-m-d H:i:s'),
        ];
        $res = $this->save($data, $where);
        if ($res === false) {
            return $this->result(1, '更新用户信息失败');
        }

        $res_data = [
            'uid'        => $uid,
            'nick_name'  => $nick_name,
            'avatar_url' => $avatar_url,
        ];
        return $this->result(0, '', $res_data);
    }

}
